package com.zhipu.balance.ui.vm.home.calculate.rapeseed

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRapeseed
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 油菜籽热损伤粒
 */
class RapeseedDamageViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 油菜籽不实体
     */
    var mRecordRapeseed : RecordRapeseed? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 热损伤粒数
     */
    var thermalDamageNumber1 = ObservableField("")


    /**
     * 热损伤粒数2
     */
    var thermalDamageNumber2 =  ObservableField("")


    /**
     * 热损伤粒-结果
     */
    var thermalDamageGrain =  ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRapeseed?.thermalDamageNumberRecorder)) {
            //热损伤粒数
            thermalDamageNumber1.set(mRecordRapeseed?.thermalDamageNumber1.toString())
            //热损伤粒数2
            thermalDamageNumber2.set(mRecordRapeseed?.thermalDamageNumber2.toString())
            //热损伤粒-结果
            thermalDamageGrain.set(mRecordRapeseed?.thermalDamageGrain.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //热损伤粒数
        thermalDamageNumber1.set("")
        //热损伤粒数2
        thermalDamageNumber2.set("")
        //热损伤粒-结果
        thermalDamageGrain.set("")
    }




    /**
     * 计算
     */
    fun calculation(){
        if(TextUtils.isEmpty(thermalDamageNumber1.get())){
            thermalDamageNumber1.set("0")
            if(TextUtils.isEmpty(thermalDamageNumber2.get())){
                thermalDamageNumber2.set("0")
            }
        }
        var mThermalDamageNumber1 = thermalDamageNumber1.get()!!.toDouble()
        if (mThermalDamageNumber1 > 0) {
            //
            mRecordRapeseed!!.thermalDamageNumber1 = mThermalDamageNumber1.toInt()
            //热损伤粒数2
            val thermalDamageNumber2 = thermalDamageNumber1.get()!!.toDouble()
            //
            mRecordRapeseed!!.thermalDamageNumber2 = thermalDamageNumber2.toInt()
            //热损伤粒-结果
            var mThermalDamageGrain = CommonUtils.formatDouble(((mThermalDamageNumber1 + thermalDamageNumber2) / 1000) * 100)
            mRecordRapeseed!!.thermalDamageGrain = mThermalDamageGrain
            thermalDamageGrain.set(mThermalDamageGrain.toString())
        }else{
            //
            mRecordRapeseed!!.thermalDamageNumber1 = mThermalDamageNumber1.toInt()
            //热损伤粒-结果
            val mThermalDamageGrain = (0 / 1000 * 100).toDouble()
            mRecordRapeseed!!.thermalDamageGrain = mThermalDamageGrain
            thermalDamageGrain.set(mThermalDamageGrain.toString())
        }
        /**
         * 热损伤粒数记录员
         */
        mRecordRapeseed!!.thermalDamageNumberRecorder = mPersonnel!!.name
    }
}